package com.example.threaddemo.other;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * 阶乘和斐波那契数列
 * 使用循环，而不使用递归
 * 递归，方法有压栈，出栈的过程，消耗性能，且容易 OOM
 */
public class FibonacciDemo {
    public static void main(String[] args) {
        // 当  -128 <= i <= 127 的过程，则取缓存的数据，否则开辟新内存。
        Integer i = 126; //自动装箱
        //等价
        Integer.valueOf(126);
        //等价
        Integer a = new Integer(126);
        int b = 126; //自动拆箱，有一个值比较的过程

        HashSet set = new HashSet();
        set.add("a");

        Collections.synchronizedMap(new HashMap());

        System.out.println("-------------------------------");
        System.out.println(Runtime.getRuntime().totalMemory()/1024/1024);
        System.out.println(Runtime.getRuntime().maxMemory()/1024/1024);
        System.out.println(Runtime.getRuntime().availableProcessors());
        System.out.println("-------------------------------");

        System.out.println(getn(1));
        System.out.println(getn(2));
        System.out.println(getn(3));
        System.out.println(getn(4));
        System.out.println(getn(5));
        System.out.println("******************");
        System.out.println(getFibonacci(3));
        System.out.println(getFibonacci(4));
        System.out.println(getFibonacci(5));
        System.out.println(getFibonacci(6));
        System.out.println(getFibonacci(7));
    }

    public static int getn(int n) {
        int i = 1;
        int result = 1;
        while (i < n) {
            result = result * i;
            i++;
        }
        result = result * n;
        return result;
    }

    public static int getFibonacci(int n) {
        int first = 0;
        int second = 1;
        int result = 0;

        for (int i = 2; i <= n; i++) {
            result = first + second;
            first = second;
            second = result;
        }

        return result;
    }
}
